package com.ysd.lis.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysIntf;
import com.ysd.lis.entity.SysIntfItem;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.mapper.sys.SysIntfItemMapper;
import com.ysd.lis.mapper.sys.SysIntfMapper;
import com.ysd.lis.service.SysIntfService;
import com.ysd.util.RedisUserManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.*;

@Service
public class SysIntfServiceImpl extends ServiceImpl<SysIntfMapper, SysIntf> implements SysIntfService {
    @Autowired
    SysIntfMapper sysIntfMapper;
    @Autowired
    SysIntfItemMapper sysIntfItemMapper;

    public Result getIntfList(SysIntf sysIntf){
        Map returnMap = new HashMap<>();
        LambdaQueryWrapper<SysIntf> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotBlank(sysIntf.getModule())) {
            lambdaQueryWrapper.eq(SysIntf ::getModule, sysIntf.getModule());
        } else {
            SysUser sysUser =  RedisUserManager.getUser();
            if(StringUtils.isNotBlank(sysUser.getOrgId())){
                lambdaQueryWrapper.eq(SysIntf ::getOrgId, sysUser.getOrgId());
            }
        }
        lambdaQueryWrapper.eq(SysIntf ::getDelFlag, 0);
        List<SysIntf>  intfList = sysIntfMapper.selectList(lambdaQueryWrapper);
        returnMap.put("sysIntfList",intfList);
        return Result.succ(1,"获取信息成功",returnMap);
    }

    public Result addIntf(SysIntf sysIntf){
        sysIntfMapper.insert(sysIntf);
        return Result.succ(1,"修改成功",sysIntf.getId());
    }
    public Result editIntf(SysIntf sysIntf){
        sysIntfMapper.updateById(sysIntf);
        return Result.succ(1,"修改成功",sysIntf);
    }

    public Result deleteIntf(String id){
        SysIntf sysIntf = sysIntfMapper.selectById(id);
        sysIntf.setDelFlag(1);
        if(sysIntfMapper.updateById(sysIntf)>0){
            return  Result.succ("删除成功");
        }else{
            return Result.fail("删除失败");
        }
    }

    public Result getIntfItemList(SysIntfItem sysIntfItem){
        Map returnMap = new HashMap<>();
        LambdaQueryWrapper<SysIntfItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(sysIntfItem.getIntfId())) {
            lambdaQueryWrapper.eq(SysIntfItem ::getIntfId, sysIntfItem.getIntfId());
        }
        lambdaQueryWrapper.eq(SysIntfItem ::getDelFlag, 0);
        List<SysIntfItem> intfItemList = sysIntfItemMapper.selectList(lambdaQueryWrapper);
        returnMap.put("sysIntfItemList",intfItemList);
        return Result.succ(1,"获取信息成功",returnMap);
    }

    public Result getIntfItem(SysIntfItem sysIntfItem){
        if (StringUtils.isBlank(sysIntfItem.getId())) {
            return Result.fail("参数不全");
        }
        SysIntfItem sysIntfItemDb = sysIntfItemMapper.selectById(sysIntfItem.getId());
        return Result.succ(1,"获取信息成功",sysIntfItemDb);
    }

    public Result addIntfItem(SysIntfItem sysIntfItem){
        sysIntfItemMapper.insert(sysIntfItem);
        return Result.succ(1,"修改成功",sysIntfItem.getId());
    }
    public Result editIntfItem(SysIntfItem sysIntfItem){
        sysIntfItemMapper.updateById(sysIntfItem);
        return Result.succ(1,"修改成功",sysIntfItem);
    }

    public Result deleteIntfItem(String id){
        SysIntfItem sysIntfItem = sysIntfItemMapper.selectById(id);
        sysIntfItem.setDelFlag(1);
        if(sysIntfItemMapper.updateById(sysIntfItem)>0){
            return  Result.succ("删除成功");
        }else{
            return Result.fail("删除失败");
        }
    }
    public Result saveIntfItemSettings(SysIntfItem sysIntfItem){
        if (StringUtils.isBlank(sysIntfItem.getId())) {
            return Result.fail("参数不全");
        }
        SysIntfItem sysIntfItemDb = sysIntfItemMapper.selectById(sysIntfItem.getId());
        sysIntfItemDb.setSettings(sysIntfItem.getSettings());
        sysIntfItemMapper.updateById(sysIntfItemDb);
        return Result.succ(1,"修改成功",sysIntfItemDb);
    }


    public Result copyIntfs(SysIntf sysIntf){
        if (StringUtils.isBlank(sysIntf.getModule())) {
            return Result.fail("未获取操作模块");
        }
        if (sysIntf.getSysIntfs() == null || sysIntf.getSysIntfs().size() == 0) {
            return Result.fail("未获取操作接口");
        }
        for (SysIntf intf : sysIntf.getSysIntfs()) {
            SysIntf sysIntfDb = sysIntfMapper.selectById(intf.getId());
            SysIntf sysIntfNew = new SysIntf();
            BeanUtils.copyProperties(sysIntfDb,sysIntfNew);
            sysIntfNew.setId("");
            sysIntfNew.setOrgId(sysIntf.getOrgId());
            sysIntfNew.setModule(sysIntf.getModule());
            sysIntfNew.setState(0);
            sysIntfMapper.insert(sysIntfNew);

            LambdaQueryWrapper<SysIntfItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SysIntfItem ::getIntfId, intf.getId());
            lambdaQueryWrapper.eq(SysIntfItem ::getDelFlag, 0);
            List<SysIntfItem> intfItemList = sysIntfItemMapper.selectList(lambdaQueryWrapper);
            for (SysIntfItem sysIntfItem : intfItemList) {
                SysIntfItem sysIntfItemNew = new SysIntfItem();
                BeanUtils.copyProperties(sysIntfItem, sysIntfItemNew);
                sysIntfItemNew.setId("");
                sysIntfItemNew.setIntfId(sysIntfNew.getId());
                sysIntfItemNew.setState(0);
                sysIntfItemMapper.insert(sysIntfItemNew);
            }
        }
        return Result.succ(1,"修改成功",sysIntf.getId());
    }

    public Result getSource(SysIntfItem sysIntfItem) {
        if (StringUtils.isBlank(sysIntfItem.getId())) {
            return Result.fail("参数不全");
        }
        SysIntfItem sysIntfItemDb = sysIntfItemMapper.selectById(sysIntfItem.getId());
        if (StringUtils.isBlank(sysIntfItemDb.getSource())) {
            return Result.fail("未配置源表信息");
        }
        SysIntf sysIntfDb = sysIntfMapper.selectById(sysIntfItemDb.getIntfId());
        List<String> list = new ArrayList();
        if ("db".equalsIgnoreCase(sysIntfDb.getType())) {
            try {
                execDb(sysIntfDb.getDbType(),sysIntfDb.getDbAddr(),sysIntfDb.getDbName(),
                        sysIntfDb.getDbUsername(),sysIntfDb.getDbPassword(),sysIntfItemDb.getSource(), list);
            } catch (Exception e) {
                return Result.fail(e.getMessage());
            }
        }
        return Result.succ(1,"修改成功",list);
    }

    public void execDb(String dbType, String dbAddr, String dbName, String dbUsername, String dbPassword, String source, List<String> list) throws Exception {
        if (StringUtils.isBlank(dbType)) {
            throw new ArithmeticException("未配置数据库类型");
        }
        if (StringUtils.isBlank(dbAddr)) {
            throw new ArithmeticException("未配置数据库地址");
        }
        if (StringUtils.isBlank(dbName)) {
            throw new ArithmeticException("未配置数据库名称");
        }
        if (StringUtils.isBlank(dbUsername)) {
            throw new ArithmeticException("未配置数据库登录名");
        }
        if (StringUtils.isBlank(dbPassword)) {
            throw new ArithmeticException("未配置数据库登录密码");
        }
        if (StringUtils.isBlank(source)) {
            throw new ArithmeticException("未配置数据库数据来源表");
        }
        String sql = "";
        String url = "";
        Connection conn = null;
        Statement sta = null;
        ResultSet rs = null;
        if ("sqlserver".equalsIgnoreCase(dbType)) {
            url = "jdbc:sqlserver://" + dbAddr + ";databaseName=" + dbName;
            sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='" + source + "'";
        } else if ("oracle".equalsIgnoreCase(dbType)) {
            url = "jdbc:oracle:thin:@" + dbAddr + ":" + dbName;
            sql = "SELECT COLUMN_NAME FROM all_tab_columns WHERE TABLE_NAME = upper('" + source + "')";
        } else {
            throw new ArithmeticException("暂不支持"+dbType);
        }
        //连接数据库，执行sql
//        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection(url, dbUsername, dbPassword);
        sta = conn.createStatement();
        rs = sta.executeQuery(sql);
        while (rs.next()) {
            list.add((String) rs.getObject("COLUMN_NAME"));
        }
    }

    public static void main(String[] args) {
        String temp = "";
    }
}
